package com.auction.service.impl;

import com.auction.common.dao.MyBaseMapper;
import com.auction.common.dao.MyBaseService;
import com.auction.common.dao.MyBaseServiceImpl;
import com.auction.common.dao.bean.PageResult;
import com.auction.entity.Product;
import com.auction.entity.ProductCarousel;
import com.auction.entity.ProductDetailImages;
import com.auction.entity.ProductInfo;
import com.auction.mapper.ProductCarouselMapper;
import com.auction.mapper.ProductDetailImagesMapper;
import com.auction.mapper.ProductInfoMapper;
import com.auction.mapper.ProductMapper;
import com.auction.query.ProductQuery;
import com.auction.service.ProductService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import static com.auction.common.utils.Assert.isSuccess;

import java.util.List;
import java.util.Map;

@Service
public class ProductServiceImpl extends MyBaseServiceImpl<Product, ProductMapper> implements ProductService {
    @Autowired
    ProductMapper productMapper;

    @Autowired
    ProductCarouselMapper pcm;
    @Autowired
    ProductDetailImagesMapper pdim;
    @Autowired
    ProductInfoMapper pi;

    //修改
    @Transactional
    @Override
    public int upP(Product product) {
        //修改商品表
        isSuccess(productMapper.updateProduct(product),"修改商品表失败");
        //对轮播图表的的操作
            //获取轮播图集合
        List<ProductCarousel> carousels = product.getCarouselList();
            //先删除
        isSuccess(pcm.deleteByProductId(product.getId()),"删除轮播图表失败");
            //再添加
        if(carousels.size()>0){
            //有数据再添加
            isSuccess(pcm.insertCarousel(product.getId(),carousels),"添加轮播图失败");
        }

        //对商品详情表的操作
            //获取详情图集合
        List<ProductDetailImages> detailImages = product.getDetailImageList();
            //先删除
            isSuccess(pdim.byProductIdDelete(product.getId()),"删除详情表失败");
            if(detailImages.size()>0){
                //在添加
                isSuccess(pdim.setDetailImages(detailImages, product.getId()),"详情表插入失败");
            }

        //最后返回一
        return 1;

    }

    @Override
    public PageResult selectComm(ProductQuery productQuery) {
        PageHelper.startPage(productQuery.getPage(),productQuery.getPageSize());
        List<Map> list=productMapper.selectComm(productQuery);
        PageInfo   page  =new PageInfo(list);
        return new PageResult<>(productQuery.getPage(),productQuery.getPageSize(),page.getTotal(),list);
    }

    //重写添加添加
    @Transactional
    @Override
    public int saveEntity(Product entity) {
        //添加商品
        int r =baseMapper.insert(entity);
        System.out.println(entity.getId());
        isSuccess(r,"添加失败");
        //添加商品详情图
        if(entity.getDetailImageList().size()>0){
            isSuccess((pdim.setDetailImages(entity.getDetailImageList(),entity.getId())),"添加商品详情表失败");
        }
        //添加商品轮播图
        if(entity.getCarouselList().size()>0){
            isSuccess((pcm.insertCarousel(entity.getId(),entity.getCarouselList())),"添加商品轮播表失败");
        }
        //添加商品描述
        ProductInfo productInfo = entity.getProductInfo();
        productInfo.setP_id(entity.getId());
        isSuccess(pi.insert(productInfo),"产品描述添加失败");
        return 1;
    }



}
